/**
 * {
	"skuId": "1327971433606561794",
	"skuName": "幻夜黑  360G  16寸",
	"spuId": "1327971433522675714",
	"price": 2,
	"stock": 2,
	"defaultImage": "https://sass-gmall.oss-cn-hangzhou.aliyuncs.com/images/1605447979813.jpg",
	"skuAttrItems": {
		"颜色": "幻夜黑",
		"尺寸": "360G",
		"内存": "16寸"
	},
	"payNum": 1,
	"checked": false
}, {
	"skuId": "1327971433656893441",
	"skuName": "幻夜黑  360G  20寸",
	"spuId": "1327971433522675714",
	"price": 3,
	"stock": 3,
	"defaultImage": "https://sass-gmall.oss-cn-hangzhou.aliyuncs.com/images/1605447987108.jpg",
	"skuAttrItems": { 
		"颜色": "幻夜黑",
		"尺寸": "360G",
		"内存": "20寸"
	},
	"payNum": 1,
	"checked": false
}
			-------------------------------------------------------------------------------------
			let item={
				skuId:this.paySku.skuId,
				skuName:this.paySku.skuName,
				spuId:this.paySku.spuId,
				price:this.paySku.price,
				stock:this.paySku.stock,
				defaultImage:this.paySku.defaultImage,
				skuAttrItems:this.paySku.skuAttrItems,
				payNum:this.payNum,
				checked:false
			}
 */

export default {
	state: {
		cartList: [{
	"skuId": "1327971433606561794",
	"skuName": "幻夜黑  360G  16寸",
	"spuId": "1327971433522675714",
	"price": 2,
	"stock": 2,
	"defaultImage": "https://sass-gmall.oss-cn-hangzhou.aliyuncs.com/images/1605447979813.jpg",
	"skuAttrItems": {
		"颜色": "幻夜黑",
		"尺寸": "360G",
		"内存": "16寸"
	},
	"payNum": 1,
	"checked": false
}, {
	"skuId": "1327971433656893441",
	"skuName": "幻夜黑  360G  20寸",
	"spuId": "1327971433522675714",
	"price": 3,
	"stock": 3,
	"defaultImage": "https://sass-gmall.oss-cn-hangzhou.aliyuncs.com/images/1605447987108.jpg",
	"skuAttrItems": { 
		"颜色": "幻夜黑",
		"尺寸": "360G",
		"内存": "20寸"
	},
	"payNum": 1,
	"checked": false
}]
	},
	getters: {
		 // 判断是否全选
		 checkedAll(state){
			 if(state.cartList.length==0){
				 return true;
			 }
			 //some的好处就是：方法会依次执行数组的每个元素，如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测，如果没有满足条件的元素，则返回false。
			 return state.cartList.some(v=>{
				 if(!v.checked){
					 //return true则后续的元素不再执行
					 return true;
				 }
			 })
		 },
		 // 合计选中总数和总金额
		 totalNumAndPrice(state){
			 let entity={selectNumber:0,totlePrice:0};
			 state.cartList.reduce((res,item)=>{
				 if(item.checked){
					 res.selectNumber++;
					 res.totlePrice+=item.payNum*item.price
				 }
				  return res;
			 },entity);
			 return entity;
		 },
		// 禁用全选
		disabledSelectAllRadio(state){
			return state.cartList.length===0;
		},
		// 获取选中的值
		getSelected(state){
			return state.cartList.filter(v=>v.checked);
		}
	},
	mutations: {
		// 加入购物车
		saveCart(state, goods) {
			// state.cartList.push(goods)
			if (state.cartList.length == 0) {
				state.cartList.push(goods);
			} else {
				let flg = state.cartList.some((item) => {
					if (item.skuId === goods.skuId) {
						item.payNum += goods.payNum;
						return true;
					}
				});
				console.log(flg);
				if (!flg) {
					state.cartList.push(goods);
				}

			}
			console.log(state.cartList)
		},
		// 购物车全选
		selectAll(state){
			state.cartList.forEach(v=>v.checked=true)
		},
		// 取消全选
		canclSelectAll(state){
			state.cartList.forEach(v=>v.checked=false);
		},
		// 取消和选中某一项
		clickRadio(state,indx){
			state.cartList[indx].checked=!state.cartList[indx].checked
		},
		// 删除购物车中的商品
		deleteCart(state){
			state.cartList=state.cartList.filter(v=>!v.checked);
			uni.showToast({
				title:"删除成功"
			})
		}
	},
	actions:{
		/**
		 * actions和mutations都是对state中的数据修改，但mutations是同步修改，而actions是异步修改
		 * 在actions中的方法，vuex会自动给我们传来commit和getters 
		 */
		/**
		 * 最底部单选按钮全选或全取消
		 */
		doSelectAll({commit,getters}){
			//调用getters中的方法
			let flg=getters.checkedAll;
			console.log(flg+"--------------------------------")
			if(!flg){
				//表示当前所有商品已经全部选中了
				commit("canclSelectAll")//执行mutations中的方法
			}else{
				//表示当前商品没有全部选中
				commit("selectAll")//执行mutations中的方法
			}
		},
		doDeleteGoods({commit}){
			uni.showModal({
				content:"是否确认删除",
				success:(res)=>{
					if(res.confirm){
						commit("deleteCart")
					}
				}
			})
		}
	}
}
